﻿#include "SemiAutoMeasureDockWidget.h"
#include "ui_SemiAutoMeasureDockWidget.h"
#include <QFileDialog>
SemiAutoMeasureDockWidget::SemiAutoMeasureDockWidget(QWidget *parent) :
    QDockWidget(parent),
    ui(new Ui::SemiAutoMeasureDockWidget)
{
    ui->setupUi(this);
    m_pDatabase = new SQLiteImplement2;
}

SemiAutoMeasureDockWidget::~SemiAutoMeasureDockWidget()
{
    delete ui;
}

void SemiAutoMeasureDockWidget::on_pushButtonLoadRecipe_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this,
                                                         u8"选择数据库文件",
                                                         "D:/SQLite");
    if(fileName.isEmpty()) return;

    m_pDatabase->loadRecipe(m_pData, fileName);
    data2GUI();

}

void SemiAutoMeasureDockWidget::data2GUI()
{
    MeasurePair &p = m_pData->currentMeasure();
    ui->labelRecipeName->setText(m_pData->recipeName());
    ui->labelDescription->setText(m_pData->description());
    int count = m_pData->count();
    ui->labelCount->setNum(count);

    ui->labelIndex->setNum(m_pData->index());
    ui->labelCL->setText(QString::number(p.CL));
    ui->labelLCL->setText(QString::number(p.LCL));
    ui->labelUCL->setText(QString::number(p.UCL));
    ui->labelUSL->setText(QString::number(p.USL));
    ui->labelLSL->setText(QString::number(p.LSL));
    ui->labelValue->setText(QString::number(p.value_mm));
}

void SemiAutoMeasureDockWidget::GUI2Data()
{
    MeasurePair &p = m_pData->currentMeasure();
    double v = ui->labelValue->text().toDouble();
    p.value_mm  = v;
}

void SemiAutoMeasureDockWidget::attachStageControl(StageControl *control)
{
    m_pStageControl = control;
}

void SemiAutoMeasureDockWidget::attachData(MeasurementData *data)
{
    m_pData = data;
}

void SemiAutoMeasureDockWidget::attachCamera(TisCamera *camera)
{
    m_pCamera = camera;
}

void SemiAutoMeasureDockWidget::on_pushButtonStart_clicked()
{
    m_pData->moveTo(0);
    data2GUI();
}

void SemiAutoMeasureDockWidget::on_pushButtonForward_clicked()
{
    GUI2Data();
    m_pData->next();
    data2GUI();
}

void SemiAutoMeasureDockWidget::on_pushButtonBackward_clicked()
{
    GUI2Data();
    m_pData->prev();
    data2GUI();
}

void SemiAutoMeasureDockWidget::on_pushButtonGotoStart_clicked()
{
    MeasurePair &p = m_pData->currentMeasure();
    double x = p.start.x_mm;
    double y = p.start.y_mm;
    double z = p.start.z_mm;
    double exp = p.start.exposure;

    int top = p.start.topLampIntensity;
    int bottom = p.start.bottomLampIntensity;

    m_pStageControl->moveToPos(x, y, z, 100);
    m_pCamera->setExpoureTime(exp);
}

void SemiAutoMeasureDockWidget::on_pushButtonGotoEnd_clicked()
{
    MeasurePair &p = m_pData->currentMeasure();
    double x = p.end.x_mm;
    double y = p.end.y_mm;
    double z = p.end.z_mm;
    double exp = p.end.exposure;

    int top = p.end.topLampIntensity;
    int bottom = p.end.bottomLampIntensity;

    m_pStageControl->moveToPos(x, y, z, 100);
    m_pCamera->setExpoureTime(exp);
}
